<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>超管端</title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"
        integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
        crossorigin="anonymous"></script>
    <script>
        var tokenID;
        var rootURL = "http://127.0.0.1:8080"
        var orderID = 0;
        var myProduct = [];
        var click = false;
        $(document).ready(function () {//获取信息
            tokenID = getQueryString("tokenID");
            get("/admin/getInitMessage");
        });
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
            var context = "";
            if (r != null)
                context = decodeURIComponent(r[2]);
            reg = null;
            r = null;
            return context == null || context == "" || context == "undefined" ? "" : context;
        }
        function mySubmit(id) {//删除分类
            get("/myData/delSentenceType?type_id=" + id);
        }
        function getKeywordsByTypeID(keywordTypeList, type_id) {
            var keywords = [];
            for (var i = 0; i < keywordTypeList.length; i++) {
                let keyword = keywordTypeList[i];
                if (keyword.type_id == type_id) {
                    keywords.push(keyword);
                }
            }
            return keywords;
        }
        function upDateOrderMessage(myTrees, keywordTypeList) {
            for (var i = 0; i < myTrees.length; i++) {
                var myTree = myTrees[i];
                let type_id = myTree.type_id;//类别id
                let title = myTree.title;//类别名称
                let sentence_nub = myTree.sentence_nub;//语句数量
                if (type_id > orderID) {
                    orderID = type_id;
                    let keywords = getKeywordsByTypeID(keywordTypeList, type_id);//所有关键词
                    var tr = $("<tr></tr>").attr("id", "t" + type_id);
                    var td = $("<td>分类：" + title + "</td><td>样本数量：" + sentence_nub + "</td>");
                    tr.append(td);
                    if (keywords != null) {
                        for (var j = 0; j < keywords.length; j++) {
                            let keyword = keywords[j];
                            let keyword_mes = keyword.keyword_mes;//订单信息
                            let answer = keyword.answer;//回答问题
                            var oper = $("<td>关键词信息：" + keyword_mes + "</td><td>缺失回复：" + answer + "</td>");
                            tr.append(oper);
                        }
                    }
                    var btn = $("<button>删除分类</button>").attr("type", "button").attr("class", "btn btn-primary").attr("onclick", "mySubmit(" + type_id + ")");
                    var btd = $("<td></td>").append(btn);
                    tr.append(btd);
                    $("#orderMessage").append(tr);
                }
            }

        }
        function passAdmin(adminID) {//通过账号审核
            let pass = {
                "id": adminID,
                "agree": true
            }
            post(pass, "/admin/pass");
        }
        function notPassAdmin(adminID) {//不通过账号审核
            let pass = {
                "id": adminID,
                "agree": false
            }
            post(pass, "/admin/pass");

        }

        function agreeAdmin(myAdmins) {
            for (var i = 0; i < myAdmins.length; i++) {
                let admin = myAdmins[i];
                var tr = $("<tr></tr>").attr("id", "a" + admin.id);
                var td = $("<td>" + admin.id + "</td><td>" + admin.name + "</td>");
                var yBtn = $("<button>通过</button>").attr("type", "button").attr("class", "btn btn-primary").attr("onclick", "passAdmin(" + admin.id + ")");
                var nBtn = $("<button>否决</button>").attr("type", "button").attr("class", "btn btn-default").attr("onclick", "notPassAdmin(" + admin.id + ")");
                var btd = $("<td></td>").append(yBtn).append(nBtn);
                tr.append(td).append(btd);
                $("#accountMessage").append(tr);
            }
        }
        function selectMessage(message) {
            var myTrees = message.myTrees;//当前语言分类
            var keywordTypeList = message.keywordTypeList;//关键词类别信息
            var myAdmins = message.admins;//待通过账号
            if (myTrees != null) {
                upDateOrderMessage(myTrees, keywordTypeList);
            }
            if (myAdmins != null) {
                agreeAdmin(myAdmins);
            }
        }
        function delPass(resultID) {
            $("#accountMessage").find("#a" + resultID).remove();
        }
        function delSentenceType(resultID) {
            $("#orderMessage").find("#t" + resultID).remove();
        }
        function get(url) {
            let xhr = new XMLHttpRequest();
            xhr.open('GET', rootURL + url);
            xhr.setRequestHeader('tokenID', tokenID);
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {//固定写法
                    var response = xhr.responseText;
                    var message = JSON.parse(response);//返回数据
                    if (message.error == 0) {//加载当前已有分类和关键词
                        if (message.responseType == 2) {
                            selectMessage(message);
                        } else {//删除语句分类成功
                            delSentenceType(message.resultID);
                        }
                    } else {
                        alert("错误：" + message.errorMessage);
                    }
                }
            }
        }
        function post(data, url) {//post请求
            let xh = new XMLHttpRequest();
            xh.onreadystatechange = function () {
                if (xh.readyState == 4 && (xh.status >= 200 && xh.status < 400)) {
                    var response = xh.responseText;
                    var message = JSON.parse(response);//返回数据
                    if (message.error == 0) {
                        if (message.responseType == 4) {
                            delPass(message.resultID);
                        } else if (message.responseType == 5) {//添加语句关键词类别
                            myProduct = [];
                            click = false;
                            get("/admin/getInitMessage");
                        }
                    } else {//弹出异常
                        alert("错误：" + message.errorMessage);
                    }
                }
            };
            xh.open("POST", rootURL + url);
            xh.setRequestHeader('Content-Type', 'application/json');
            xh.setRequestHeader('tokenID', tokenID);
            xh.send(JSON.stringify(data));
        }
        function addProduct() {//添加产品
            let sentence = $("#mySentence").val();
            let keyword = $("#mykeyword").val();
            let myAnswer = $("#myAnswer").val();
            if (sentence != '' && keyword != '' && myAnswer != '' && !click) {
                let proText = keyword + "#" + myAnswer;//产品文本
                let params = {
                    "keyword_mes": keyword,
                    "answer": myAnswer
                }
                myProduct.push(params);
                if ($("#products").text() == '') {
                    $("#products").text(proText);
                } else {
                    $("#products").text($("#products").text() + "," + proText)
                }
            } else {
                alert("信息不全！");
            }
        }
        function delProduct() {//删除产品
            if (myProduct.length > 0 && !click) {
                myProduct.pop();
                let txt = $("#products").text();
                let tm = txt.split(",");
                let ntxt = '';
                for (var i = 0; i < tm.length - 1; i++) {
                    ntxt = ntxt + tm[i];
                    if (i < tm.length - 2) {
                        ntxt = ntxt + ",";
                    }
                }
                $("#products").text(ntxt);
            }
        }
        function submit() {//提交添加的信息
            let sentence = $("#mySentence").val();
            if (sentence != '' && !click) {
                click = true;
                var params = {
                    "title": sentence,
                    "keywordTypeMessages": []
                }
                params.keywordTypeMessages = myProduct;
                post(params, "/myData/addSentenceType");
            } else {
                alert("缺失语句分类信息");
            }
        }
    </script>
</head>

<body style="overflow-x: hidden;">
    <div class="row">
        <div class="col-md-8">
            <table class="table  table-hover" id="orderMessage">
                <caption style="text-align: center;">
                    <h3>分类列表 <span class="label label-default">刷新更新</span></h3>
                </caption>
            </table>
        </div>
        <div class="col-md-4">
            <h3 for="basic-url" style="text-align: center;">添加语义分类:</h3>
            <div class="panel panel-default">
                <div class="panel-body" id="products"></div>
            </div>
            <div class="input-group input-group-lg" style="margin-top: 20px;">
                <span class="input-group-addon">语句分类：</span>
                <input type="text" placeholder="添加语句分类描述" class="form-control" id="mySentence"
                    aria-describedby="basic-addon1">
            </div>
            <div class="input-group input-group-lg" style="margin-top: 20px;">
                <span class="input-group-addon">关键词添加：</span>
                <input type="text" placeholder="添加关键词描述" class="form-control" id="mykeyword"
                    aria-describedby="basic-addon1">
            </div>
            <div class="input-group input-group-lg" style="margin-top: 20px;">
                <span class="input-group-addon">缺失回复：</span>
                <input type="text" placeholder="该关键词缺失回复" class="form-control" id="myAnswer"
                    aria-describedby="basic-addon1">
            </div>
            <div class="btn-group btn-group-justified" role="group" style="margin-top: 20px;border-radius: 5px;">
                <div class="btn-group" role="group">
                    <button type="button" class="btn btn-success" onclick="addProduct()">添加关键词</button>
                </div>
                <div class="btn-group" role="group">
                    <button type="button" class="btn btn-danger" onclick="delProduct()">删除</button>
                </div>
            </div>
            <div class="btn-group btn-group-justified" role="group" style="margin-top: 20px;border-radius: 5px;">
                <div class="btn-group" role="group">
                    <button type="button" class="btn btn-primary" onclick="submit()">提交新的语句分类</button>
                </div>
            </div>
            <table class="table  table-hover" id="accountMessage">
                <caption style="text-align: center;">
                    <h3>账号申请 <span class="label label-default">刷新更新</span></h3>
                </caption>
                <tr>
                    <td>id</td>
                    <td>姓名</td>
                    <td>是否通过</td>
                </tr>
            </table>
        </div>
    </div>
</body>

</html>